package se.webbzon.boltzmann.game.object.dialogue;

import java.util.Iterator;
import java.util.List;

import se.webbzon.boltzmann.game.object.dialogue.layout.SimpleDialogueLayout;
import se.webbzon.boltzmann.game.object.dialogue.word.AbstractDialogueWord;
import se.webbzon.boltzmann.input.Input;
import se.webbzon.boltzmann.input.Button;
import se.webbzon.oschi01.worldarea.WorldArea;
import se.webbzon.oschi01.worldobject.appearance.SimpleGroupAppearance3D;

public class SimpleDialogue extends AbstractDialogue {
	
	// The continue circle
	private final DialogueCircle circle;
	
	// Calls all the listeners in the next step if set
	private volatile boolean proceed;
		
	// Shows all the words in the next step if set
	private volatile boolean showAll;
	
	/*============================================================
	Constructors
	============================================================*/

	/** Creates a new simple dialogue. The words in the dialogue 
	 * will be parsed from the given text. **/
	public SimpleDialogue(	SimpleDialogueLayout layout, 
							DialogueBackground background,
							String text) {
		this(layout,background,DialogueTextParser.parse(layout,text));
	}
	
	/** Creates a new abstract dialogue with a given list of words. **/
	public SimpleDialogue(	SimpleDialogueLayout layout, 
							DialogueBackground background,
							List<AbstractDialogueWord> words) {
		super(layout,background,words);
		
		showAll = false;
		proceed = false;
		
		circle = new DialogueCircle(layout.getCircleRadius());
		circle.setPosition(	layout.getWidth()/2 - layout.getCircleRadius()*2, 
							layout.getCircleYSpacing(), 
							0);
		circle.hide();
		
		((SimpleGroupAppearance3D) getAppearance()).addAppearance(circle.getAppearance());
	}

	/*============================================================
	Public Methods
	============================================================*/
	
	@Override public void hide() {
		super.hide();
		proceed = false;
		showAll = false;
	}
	
	@Override public void onStep(WorldArea world) {
		super.onStep(world);
		if (proceed) {
			proceed = false;
			notifyOnContinue();
		} else if (showAll) {
			showAll = false;
			showAllWords();
		}
	}
	
	@Override public void onShowInput() {
		circle.display();
	}
	
	@Override public void onHideInput() {
		circle.hide();
	}
	
	@Override public void primaryAxisChanged(Input input, double direction, double value) {};

	@Override public void secondaryAxisChanged(Input input, double direction, double value) {};

	@Override public void buttonPressed(Input input, Button button) {
		if (isInputReady() && button == Button.ACTION) {
			if (isAllVisible())
				proceed = true;
			else
				showAll = true;
		}
	};

	@Override public void buttonReleased(Input input, Button button) {};
	
	@Override public void bindingAccepted(Input input, Button button) {};
	
	/*============================================================
	Private Methods
	============================================================*/
	
	/** Notifies all listeners to continue. **/
	private void notifyOnContinue() {
		Iterator<DialogueListener> i = getListeners().iterator();
		while (i.hasNext()) {
			DialogueListener listener = i.next();
			if (listener instanceof SimpleDialogueListener)
				((SimpleDialogueListener) listener).onContinue(this);
		}
	}

}
